/**
 * 标准列表
 * @author linfeng
 * @since 2019/12/24
 */
let layer = layui.layer;

layui.extend({
    zmnExportUtil: '{/}/static/js/zmn.excel.util'
}).define(['index', 'form', 'table', 'laypage', 'layedit','zmnExportUtil','laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var table = layui.table;
    var laypage = layui.laypage;
    let layDate = layui.laydate;
    var layedit = layui.layedit;
    var zmnExportUtil =layui.zmnExportUtil;

    layDate.render({
        elem: '#startAndEndDate',
        range: true
    });

    form.render();

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        defaultToolbar: json.showExportExcel ? ['exports'] : [''],
        limit: json.pageSize,
        height: 'full-190',
        done: function () {
            $("div[lay-event='LAYTABLE_EXPORT']").attr('lay-event', 'CUSTOM_EXPORT');
        },
    });

    // 监听导出事件
    form.on('submit(query-export)', function (data) {
        let index = layer.load(1, {
            shade: [0.5, '#DBDBDB'] //0.1透明度的白色背景
        });
        zmnUtil.downloadLimitByConfig("/standard/record/exportExcel.action?" + $("#standard_record_list").serialize(), "标准文库管理列表.xlsx", function () {
            layer.close(index);
        });
        return false;
    });

    laypage.render(layui.$.extend({limits:[10, 20, 30, 40, 50,100]},zmnPage.laypageOptions(json)));

    var ids = new Array();

    // 选择（全选或单选）
    table.on("checkbox(zmn-table)", function (obj) {
        ids = table.checkStatus('zmn-table').data.map((item) => item.standardId);
    });

    // 启用/停用
    table.on("toolbar(zmn-table)", function (obj) {
        //启用停用
        if (obj.event == "start" || obj.event == "stop") {
            if (ids.length === 0) {
                return layer.alert("请选择至少1条数据！");
            }
            zmnPage.layStartStop(obj, "/standard/record/updateStatus.action", ids.join(","));
        }

        // 表格导出
        if (obj.event == 'CUSTOM_EXPORT') {
            $("#query-export").click();
        }

    });

    form.on("switch(chk_one_status)", function (obj) {
        let  url = "/standard/record/updateStatus.action";
        let standardId = obj.elem.getAttribute("key");
        let status = obj.elem.checked ? 2 : 1;
        switch (status) {
            case 1:
                zmnPage.ajaxSimple(url, {ids: standardId, status: 1}, 1000);
                break;
            case 2:
                zmnPage.ajaxSimple(url, {ids: standardId, status: 2}, 1000);
                break;
        }
        setTimeout(function () {
            location.reload(true);
        }, 3000);
    });

    // 2级部门联动
    form.on('select(deptOneId)', function(obj){
        triggerTwoDeptOptions(obj.value);
    })

    function triggerTwoDeptOptions(value){
        let twoDeptOptions = json.twoDepts.filter(p => p.parentId == value).map(p => {
            return p.deptId == json.vo.deptTwoId ? '<option value="'+p.deptId+'" selected="selected">'+p.deptName+'</option>' : '<option value="'+p.deptId+'">'+p.deptName+'</option>';
        });
        twoDeptOptions.unshift('<option value=""></option>');
        $('#deptTwoId').html(twoDeptOptions);
        form.render("select");
    }

    // 修改排序和首字母
    table.on("edit(zmn-table)", function (obj) {
        if(obj.field=='sort'){
            zmnPage.laySortPositiveInteger(obj.value,"/standard/record/updateSort.action",obj.data.standardId,100000,1000);
        }
    });
    $("#table_body").on('click', '.updateClass', function () {
        var $button = $(this);
        var standardId = $button.data('cateid');
        return zmnLayUtil.openIframeCustom(layer, "/extension/category/detail.action?standardId="+standardId, {title: "修改分类", shade: 0.3, area:["550px", "375px"]});
    });
    //对外暴露的接口
    exports('standard_record_list_js', {});
});

function downLoadFiles(standardId) {
    var judegUrl = "/standard/record/countFiles.action";
    var url = "/standard/record/downLoadFiles.action?standardId=" + standardId;
    $.ajax({
        async: false,
        type: "GET", url: judegUrl,
        dataType: "json", data: {standardId: standardId},
        success: function (json) {
            if (json.status != 200) {
                layer.msg(json.message);
                return;
            } else if (json.status == 200) {
                window.open(url, "_parent");
                return;
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        },
        complete:function () {
        }
    });

    return false;
}

